package com.ruoyi.web.controller.uav;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.uav.domain.UavTeachingMaterial;
import com.ruoyi.uav.service.IUavTeachingMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

/**
 * 教材信息Controller
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@RestController
@RequestMapping("/uav/material")
@Api(value = "教材信息控制层", tags = "教材信息控制层")
public class UavTeachingMaterialController extends BaseController {
    @Resource
    private IUavTeachingMaterialService uavTeachingMaterialService;

    /**
     * 查询教材信息列表
     */
    @PreAuthorize("@ss.hasPermi('uav:material:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询教材信息列表", notes = "传入uavTeachingMaterial")
    public TableDataInfo list(UavTeachingMaterial uavTeachingMaterial) {
        startPage();
        List<UavTeachingMaterial> list = uavTeachingMaterialService.selectUavTeachingMaterialList(uavTeachingMaterial);
        return getDataTable(list);
    }

    /**
     * 导出教材信息列表
     */
    @PreAuthorize("@ss.hasPermi('uav:material:export')")
    @Log(title = "教材信息", businessType = BusinessType.EXPORT)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @GetMapping("/export")
    @ApiOperation(value = "导出教材信息列表", notes = "传入uavTeachingMaterial")
    public AjaxResult export(UavTeachingMaterial uavTeachingMaterial) {
        List<UavTeachingMaterial> list = uavTeachingMaterialService.selectUavTeachingMaterialList(uavTeachingMaterial);
        ExcelUtil<UavTeachingMaterial> util = new ExcelUtil<UavTeachingMaterial>(UavTeachingMaterial.class);
        return util.exportExcel(list, "教材信息数据");
    }

    /**
     * 获取教材信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('uav:material:query')")
    @GetMapping(value = "/{teachingMaterialId}")
    @ApiOperation(value = "获取教材信息详细信息", notes = "传入teachingMaterialId")
    public R<UavTeachingMaterial> getInfo(@PathVariable("teachingMaterialId") Long teachingMaterialId) {
        return R.ok(uavTeachingMaterialService.getById(teachingMaterialId));
    }

    /**
     * 新增教材信息
     */
    @PreAuthorize("@ss.hasPermi('uav:material:add')")
    @Log(title = "教材信息", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PostMapping
    @ApiOperation(value = "新增教材信息", notes = "传入uavTeachingMaterial")
    public R<Integer> add(@RequestBody UavTeachingMaterial uavTeachingMaterial) {
        UavTeachingMaterial byId = uavTeachingMaterialService.getOne(Wrappers.<UavTeachingMaterial>lambdaQuery().eq(UavTeachingMaterial::getTeachingMaterialName,uavTeachingMaterial.getTeachingMaterialName()));
        if (ObjectUtils.isEmpty(byId)){
            R.fail("教材名称已存在");
        }
        uavTeachingMaterial.setPermission("0");
        uavTeachingMaterial.setCreateBy(SecurityUtils.getUsername());
        uavTeachingMaterial.setCreateTime(DateUtils.getNowDate());
        return R.ok(uavTeachingMaterialService.getBaseMapper().insert(uavTeachingMaterial));
    }

    /**
     * 修改教材信息
     */
    @PreAuthorize("@ss.hasPermi('uav:material:edit')")
    @Log(title = "教材信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PutMapping
    @ApiOperation(value = "修改教材信息", notes = "传入uavTeachingMaterial")
    public R<Integer> edit(@RequestBody UavTeachingMaterial uavTeachingMaterial) {
        UavTeachingMaterial byId = uavTeachingMaterialService.getOne(Wrappers.<UavTeachingMaterial>lambdaQuery().eq(UavTeachingMaterial::getTeachingMaterialName,uavTeachingMaterial.getTeachingMaterialName()));
        if (ObjectUtils.isEmpty(byId)){
            R.fail("教材名称已存在");
        }
        uavTeachingMaterial.setUpdateBy(SecurityUtils.getUsername());
        uavTeachingMaterial.setUpdateTime(DateUtils.getNowDate());
        return R.ok(uavTeachingMaterialService.getBaseMapper().updateById(uavTeachingMaterial));
    }

    /**
     * 删除教材信息
     */
    @PreAuthorize("@ss.hasPermi('uav:material:remove')")
    @Log(title = "教材信息", businessType = BusinessType.DELETE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
	@DeleteMapping("/{teachingMaterialIds}")
    @ApiOperation(value = "删除教材信息", notes = "传入teachingMaterialIds")
    public R<Integer> remove(@PathVariable Long[] teachingMaterialIds) {
        return R.ok(uavTeachingMaterialService.getBaseMapper().deleteBatchIds(Arrays.asList(teachingMaterialIds)));
    }
}
